/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlacciones.ejb3.dao;

import controlacciones.ejb3.dao.entity.TbTotfacFacadeLocal;
import controlacciones.ejb3.entity.TbTotfac;
import controlacciones.ejb3.pojo.TableBeanBinding_DatosFactura;
import helper.Utiles_Convertir;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Vanessa
 */
@Stateful
public class PaginacionDAOBean implements PaginacionDAOLocal {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
    public List<TbTotfac> buscaFacturas() {
        Query query = em.createQuery("SELECT t FROM TbTotfac t");
        return (List<TbTotfac>) query.setFirstResult(pagActual*pagTamanno)
                                        .setMaxResults(pagTamanno)
                                        .getResultList();
    }

    public List<TableBeanBinding_DatosFactura> getListaFacturas( String fecha, String log_user ) {
        List<TableBeanBinding_DatosFactura> lista = null;
        List listTemp;
        String queryString = "";
        queryString = "SELECT     facsernro, facnro, CONVERT(char(10), facemifec, 103) AS facemifec, " +
                " CONVERT(char(10), facvenfec, 103) AS facvenfec, inscrinrx, nombre, id_factura, isnull(flag_registrada, 0) " +
                " FROM         tb_totfac AS t " +
                " WHERE     (CONVERT(char(10), facemifec, 103) = '"+ fecha+"')";
         Query query = em.createNativeQuery(queryString);         

          listTemp =  query.setFirstResult(pagActual*pagTamanno)
                                        .setMaxResults(pagTamanno)
                                        .getResultList();

        lista = Utiles_Convertir.convertirToTableBeanBinding_DatosFacturas(listTemp);
        return lista;
    }

    public List<TableBeanBinding_DatosFactura> getListaFacturasTOP( String fecha, String log_user ) {
        List<TableBeanBinding_DatosFactura> lista = null;
        List listTemp;
        String queryString = "";
        queryString = "SELECT   top 200  facsernro, facnro, convert(char(10),facemifec,103)," +
                " convert(char(10),facvenfec,103),inscrinrx, nombre, id_factura " +
                " FROM         tb_totfac " +
                " where convert(char(10),facemifec,103) = '" + fecha + "'";
         Query query = em.createQuery(queryString);

          listTemp =  listTemp = em.createNativeQuery(queryString)
                .getResultList();

        lista = Utiles_Convertir.convertirToTableBeanBinding_DatosFacturas(listTemp);
        return lista;
    }

    public void Totfac_iniciaConfiguracion(int pagTamanno){
        this.pagTamanno = pagTamanno;
        pagActual = 0;
    }

    public void Totfac_primero(){
        pagActual = 0;
        System.out.println("pagActual: " + pagActual);
    }

    public void Totfac_siguiente() {
        pagActual++;
        System.out.println("pagActual: " + pagActual);
    }

    public void Totfac_anterior() {
        pagActual--;
        if (pagActual < 0) {
            pagActual = 0;
        }
        System.out.println("pagActual: " + pagActual);
    }

    public void Totfac_ultimo(){
        int pagUltima = 0;

        try {           
            int num_registros = getNumRegistros();
            if (pagTamanno > 0) {
                double ultima = num_registros / pagTamanno;                
                pagUltima = (int) java.lang.Math.ceil(ultima);
                System.out.println("pagUltima: " + pagUltima);
            }
        } catch (Exception e) {
        }
        pagActual = pagUltima;
        System.out.println("pagActual: " + pagActual);
    }

    public int getNumRegistros(){
        int num = 0;

        try {
            String queryString = "";
            queryString = "SELECT    count(id_factura) " +
                    " FROM         tb_totfac  ";
            Query query = em.createNativeQuery(queryString);

            Object o = query.getSingleResult();
            System.out.println(o);
            String strnum = o.toString().substring(1, o.toString().length() - 1) ;
            System.out.println(strnum );
            num = Integer.parseInt( strnum);

            System.out.println( "Num de Registros: " + num );
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return num;
    }

    private int pagActual = 0;
    //private int maxResults;
    private int pagTamanno = 200;

    @PersistenceContext(unitName = "ControlAccionesComercial_EAR-ejbPU")
    private EntityManager em;

    @EJB
    TbTotfacFacadeLocal tbTotfacFacadeLocal;

  }
